package com.approval.approvalapp.index;

import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.XzsxList;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.approval.approvalapp.util.view.CustomTouchSwipeRefreshLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

//行政办件
public class XzsxListActivity extends BaseActivity {

    @Bind(R.id.xrv)
    XRecyclerView xrv;
    @Bind(R.id.swipeRefreshLayout)
    CustomTouchSwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.empty_ll)
    LinearLayout empty_ll;
    @Bind(R.id.empty_tv)
    TextView empty_tv;
    private int pagenum = 1;
    private List<XzsxList> xzsxLists = new ArrayList<>();
    private Adapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_xzsx_list;
    }

    @Override
    public void initView() {
        titleTv1.setText(getIntent().getStringExtra("title"));

        xrv.setPullRefreshEnabled(false);
        xrv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new Adapter();
        xrv.setAdapter(adapter);

        swipeRefreshLayout.setRefreshing(true);

        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshData();
            }
        }, 500);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        xrv.setNoMore(false);
                        swipeRefreshLayout.setRefreshing(true);
                        refreshData();

                    }
                }, 500);
            }
        });

        xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                swipeRefreshLayout.setRefreshing(false);
                pagenum = pagenum + 1;
                addData(pagenum);
            }
        });


    }

    private void addData(int pagenum) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("page", pagenum);
        jsonObject.put("org_code", SpUtil.getString(this, SpUtil.ORG_CODE, ""));
        jsonObject.put("title", getIntent().getStringExtra("title"));
        NetRequsetUtil.getInstance().netRequestPostJson("v1/user/queryItemListByType",
                jsonObject.toString()
                , new NetDownResponse() {
                    @Override
                    public void success(String str) {
                        List<XzsxList> mxzsxLists = JSON.parseArray(str, XzsxList.class);
                        if (mxzsxLists == null || mxzsxLists.size() == 0) {
                            xrv.setNoMore(true);
                        } else {
                            for (int i = 0; i < mxzsxLists.size(); i++) {
                                xzsxLists.add(mxzsxLists.get(i));
                            }
                            adapter.setXzsxLists(XzsxListActivity.this.xzsxLists);
                            xrv.loadMoreComplete();
                        }
                    }

                    @Override
                    public void errowithresponse(String str) {

                    }

                    @Override
                    public void erro() {

                    }

                    @Override
                    public void finish() {
                        if (swipeRefreshLayout != null) {
                            swipeRefreshLayout.setRefreshing(false);
                        }

                    }
                });
    }

    private void refreshData() {
        pagenum = 1;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("page", pagenum);
        jsonObject.put("org_code", SpUtil.getString(this, SpUtil.ORG_CODE, ""));
        jsonObject.put("title", getIntent().getStringExtra("title"));
        NetRequsetUtil.getInstance().netRequestPostJson("v1/user/queryItemListByType",
                jsonObject.toString()
                , new NetDownResponse() {
                    @Override
                    public void success(String str) {
                        xzsxLists = JSON.parseArray(str, XzsxList.class);
                        adapter.setXzsxLists(xzsxLists);

                        if (xzsxLists == null || xzsxLists.size() == 0){
                            empty_ll.setVisibility(View.VISIBLE);
                            empty_tv.setText("暂无" + getIntent().getStringExtra("title"));
                        }else {
                            empty_ll.setVisibility(View.GONE);
                        }

                    }

                    @Override
                    public void errowithresponse(String str) {

                    }

                    @Override
                    public void erro() {

                    }

                    @Override
                    public void finish() {
                        if (swipeRefreshLayout != null) {
                            swipeRefreshLayout.setRefreshing(false);
                        }

                    }
                });
    }

    @Override
    public void initData() {

    }

    @OnClick(R.id.back_iv)
    public void onViewClicked() {
        finish();
    }

    class Adapter extends RecyclerView.Adapter<Adapter.Holder> {
        List<XzsxList> xzsxLists;

        public void setXzsxLists(List<XzsxList> xzsxLists) {
            this.xzsxLists = xzsxLists;
            notifyDataSetChanged();
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_xzsx, parent, false);
            Holder holder = new Holder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            holder.tv1.setText(xzsxLists.get(position).getNAME());
            holder.tv3.setText(xzsxLists.get(position).getORG_NAME());

        }

        @Override
        public int getItemCount() {
            if (xzsxLists == null) {
                return 0;
            } else {
                return xzsxLists.size();
            }
        }

        class Holder extends RecyclerView.ViewHolder {
            @Bind(R.id.tv1)
            TextView tv1;
            @Bind(R.id.tv3)
            TextView tv3;
            @Bind(R.id.root)
            LinearLayout root;

            public Holder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }

    }
}
